package com.sml.validation;

import com.sml.anno.State;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

//public class StateValidation implements ConstraintValidator<"给哪个注解使用","校验的数据类型"> {
public class StateValidation implements ConstraintValidator<State,String> {

    /**
     * 初始化方法
     * @param constraintAnnotation
     */
    @Override
    public void initialize(State constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    /**
     * 校验方法
     * @param value 将来要校验的数据
     * @param context context in which the constraint is evaluated
     *
     * @return 如果返回false，校验失败，如果返回true，校验通过
     */
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        //如果value为空，直接返回true
        if(value == null){
            return false;
        }
        //如果value不为空，判断value是否为已发布或草稿
        if(value.equals("已发布") || value.equals("草稿")){
            return true;
        }
        return false;
    }
}
